<template>
  <el-dialog :title="!dataForm.id ? '新增规格' : '修改规格'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="选择属性" prop="parent_sku_id" v-if="firstInput">
        <el-select v-model="dataForm.parent_sku_id">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="输入属性值" prop="name">
        <el-input placeholder="" v-model="dataForm.name" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="order_index">
        <el-input-number v-model="dataForm.order_index" :min="1"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit('dataForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
import serviceApione from '@/api/serviceApione.js'
export default {
  data () {
    return {
      visible: false,
      firstInput: true,
      options: [
        {
          name: '顶级',
          id: 0
        }
      ],
      dataForm: {
        parent_sku_id: 0,
        name: '',
        order_index: null
      },
      dataRule: {
        parent_sku_id: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入属性名', trigger: 'blur' }
        ],
        order_index: [
          { required: true, message: '请输入排序', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {

  },
  methods: {
    init (id, parent_sku_id) {
      if (id) {
        this.dataForm.id = id
        if (parent_sku_id == 0) {
          this.firstInput = false
        } else {
          this.firstInput = true
        }
        this.skuEdital(id)
      } else {
        this.dataForm.parent_sku_id = 0
        this.firstInput = true
        this.dataForm = {
          parent_sku_id: 0,
          name: '',
          order_index: null
        }
      }
      this.topList()

      this.visible = true
    },
    skuEdital (id) {
      serviceApione.getSkuInfo(id).then(res => {
        if (res.data.code == 200) {
          this.dataForm = res.data.data
        }
      })
    },
    topList () {
      serviceApione.getSkuTop().then(res => {
        if (res.data.code == 200) {
          if (res.data.list.length > 0) {
            let arrs = [
              {
                name: '顶级',
                id: 0
              }
            ]
            this.options = arrs.concat(res.data.list)
          }
        }
      })
    },
    dataFormSubmit (formName) {
      this.$refs[formName].validate((valid) => {
        if (this.dataForm.id) {
          let parmas = {
            id: this.dataForm.id,
            name: this.dataForm.name,
            order_index: this.dataForm.order_index
          }
          serviceApione.updateSku(parmas).then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('successUp', 1)
              this.visible = false
            }
          })
        } else {
          serviceApione.createSku(this.dataForm).then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.$emit('successUp', 1)
              this.visible = false
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss">
</style>
